package sisi.payment;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * PaymentsmodeController.java - Controller class for the <i>payment mode database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 16/11/2013
 * 
 */

public class PaymentsmodeController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Paymentmode[] getPaymentmode(String cerca) {
		List<Paymentmode> paymentmode = getPaymentmodeList(cerca);
		return paymentmode.toArray(new Paymentmode[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Paymentmode> getPaymentmodeList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.paymode) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Paymentmode as c " + cWhere
							+ " order by c.paycode");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Paymentmode> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	public Paymentmode getPaymentmodexId(int id) {

		EntityManager em = getEntityManager();
		try {
			Paymentmode paymentmodex = em.find(Paymentmode.class, id);
			return paymentmodex;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Paymentmode addPaymentmode(Paymentmode paymentmode) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(paymentmode);
			em.getTransaction().commit();
			paymentmode = refreshPaymentmode(paymentmode);
		} finally {
			em.close();
			return paymentmode;
		}
	}

	@SuppressWarnings("finally")
	public boolean updatePaymentmode(Paymentmode paymentmode) {

		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Paymentmode paymentmodex = em.find(Paymentmode.class, paymentmode.getPayid());
			paymentmodex.setPayid(paymentmode.getPayid());
			paymentmodex.setPaycode(paymentmode.getPaycode());
			paymentmodex.setPaymode(paymentmode.getPaymode());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Paymentmode refreshPaymentmode(Paymentmode paymentmode) {

		Paymentmode paymentmodex = paymentmode;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			paymentmodex = em.find(Paymentmode.class, paymentmode.getPayid());
			em.flush();
			em.refresh(paymentmodex);
		} finally {
			em.close();
			return paymentmodex;
		}
	}

	@SuppressWarnings("finally")
	public boolean removePaymentmode(Paymentmode paymentmode) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Paymentmode paymentmodex = em.find(Paymentmode.class, paymentmode.getPayid());
			em.remove(paymentmodex);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
